﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Media;
using System.Diagnostics;
using System.Threading;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class MainForm
    {
                /// <summary>
        /// rebinds the controls On the OBSOLETEStaff tab When user changes the RowFilter Value
        /// </summary>
        /// <remarks>
        /// it'pShift also called at the beginning When the default filter is being set for the first Time
        /// </remarks>
        internal void STAFF_BindDetailsControls()
        {
            // clear all the existing databindings            
            foreach (Control c in tblJobDetails.Controls)
            {
                c.DataBindings.Clear();
            }
            foreach (Control c in tblPersonalDetails.Controls)
            {
                c.DataBindings.Clear();
            }
            rbMale.DataBindings.Clear();

            // first Name
            tbFirstName.DataBindings.Add("Text", SQLiteAccess.EmployeesForStaffTab, "firstName");
            tbFirstName.DataBindings[0].NullValue = String.Empty;
            // last Name
            tbLastName.DataBindings.Add("Text", SQLiteAccess.EmployeesForStaffTab, "lastName");
            tbLastName.DataBindings[0].NullValue = String.Empty;
            // phone number
            tbPhoneNumber.DataBindings.Add("Text", SQLiteAccess.EmployeesForStaffTab, "phone");
            tbPhoneNumber.DataBindings[0].NullValue = String.Empty;
            // mobile phone
            tbMobilePhone.DataBindings.Add("Text", SQLiteAccess.EmployeesForStaffTab, "mobile");
            tbMobilePhone.DataBindings[0].NullValue = String.Empty;
            // date of birth
            var dateBinding = new Binding("Text", SQLiteAccess.EmployeesForStaffTab, "birthDate");
            dateBinding.Parse += dateBinding_Parse;
            dateBinding.NullValue = DateTime.Now;
            dpBirthDate.DataBindings.Add(dateBinding);
            // address
            tbAddress.DataBindings.Add("Text", SQLiteAccess.EmployeesForStaffTab, "address");
            tbAddress.DataBindings[0].NullValue = String.Empty;
            tbNIN.DataBindings.Add("Text", SQLiteAccess.EmployeesForStaffTab, "nin");
            tbNIN.DataBindings[0].NullValue = String.Empty;
            // sex
            rbMale.DataBindings.Add("Checked", SQLiteAccess.EmployeesForStaffTab, "sex");
            rbMale.DataBindings[0].NullValue = false;
            rbMale.DataBindings[0].Format += MainForm_Format;
            // role
            var jobtitle = new Binding("Text", SQLiteAccess.EmployeesForStaffTab, "current_Position");
            jobtitle.Format += jobtitle_Format;
            lbWhatPosition.DataBindings.Add(jobtitle);

            tbContractHrs.DataBindings.Add("Text", SQLiteAccess.EmployeesForStaffTab, "current_contractHrs");
            tbContractHrs.DataBindings[0].NullValue = String.Empty;
            // holidays Allowance
            tbHolidayAllowance.DataBindings.Add("Text", SQLiteAccess.EmployeesForStaffTab, "current_holidayAllowance");
            tbHolidayAllowance.DataBindings[0].NullValue = String.Empty;
        }

        void MainForm_Format(object sender, ConvertEventArgs e)
        {
            rbFemale.Checked = !rbMale.Checked;
            rbFemale.Enabled = !rbMale.Checked;
            rbMale.Enabled = rbMale.Checked;
        }

        internal void lbContractHistory_Reload()
        {
            lbContractHistory.SuspendLayout();
            var who = lbStaffMembers.SelectedItem as DataRowView;
            if (who == null)
            {
                return;
            }
            lbContractHistory.DataSource = null;
            lbContractHistory.DataSource = SQLiteAccess.GetContractsDataView(Convert.ToInt64(who[0]));
            lbContractHistory.DisplayMember = "displayColumn";
            lbContractHistory.ValueMember = "id";
            lbContractHistory.Refresh();
            lbContractHistory.ResumeLayout();
        }

        internal void STAFF_BindControls()
        {            
            // technically we're not "reloading" it here, since its DataSource is null at this stage anyway.
            // practically it makes no difference - we populate the list.
            reload_lbStaffMembers();
            lbStaffMembers.SelectedValueChanged += (sender, e) => lbContractHistory_Reload();
            lbContractHistory_Reload();
            rbMale.CheckedChanged += rbMale_CheckedChanged;
            
            STAFF_InitializeFilterComboBoxes();
            STAFF_BindDetailsControls();

            btnEmployeesVisa.Click += (sender, e) => new VisaRestrictions().ShowDialog();
        }


        /// <summary>
        /// rebinds lbStaffMembers listbox by resetting its DataSource property
        /// </summary>
        /// <remarks>
        /// Forces updating the data. I'm honestly not sure if it'pShift not bit of a hack,
        /// but I was having problems When modifying the RowFilter of the DataView it'pShift Bound to.
        /// (if the selection was narrowed down and then *widened again*, exceptions were thrown).
        /// 
        /// It'pShift needed to recalculate the displayColumn anyway
        /// </remarks>
        void reload_lbStaffMembers()
        {
            lbStaffMembers.DataSource = null;            
            lbStaffMembers.DataSource = SQLiteAccess.EmployeesForStaffTab;            
            lbStaffMembers.DisplayMember = "displayColumn";
            lbStaffMembers.ValueMember = "id";
            SQLiteAccess.CalculateAllColumns(); // TODO: only those new ones
            lbStaffMembers.Refresh();
        }

        static List<KeyValuePair<string, string>> GetJobTitlesDataSource
        {
            get
            {
                return new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>(
                        Resources.FORM_MAINFORM_JOBPOSITIONFILTER_AllPositions_TEXTVALUE,
                        String.Empty),
                    new KeyValuePair<string, string>(
                        Resources.FORM_MAINFORM_JOBPOSITIONFILTER_ManagersDeputies_TEXTVALUE,
                        "current_Position >= " + (int)Position.StoreDeputy),
                    new KeyValuePair<string, string>(
                        Resources.FORM_MAINFORM_JOBPOSITIONFILTER_Managers_TEXTVALUE,
                        "current_Position >= " + (int)Position.AssistantManager),
                    new KeyValuePair<string, string>(
                        Resources.FORM_MAINFORM_JOBPOSITIONFILTER_StoreAssistants_TEXTVALUE,
                        "current_Position < " + (int)Position.StoreDeputy)
                };
                
            }
        }


        void btnManageHolidays_Click(object sender, EventArgs e)
        {
            if (lbStaffMembers.SelectedValue == null)
            {
                return;
            }
            var id = lbStaffMembers.SelectedValue;
            ManageHolidays((Int64)id);
        }
    }
}
